﻿

#region Using Statements

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Xml;
using System.Xml.Serialization;
using System.Threading;
using System.ComponentModel;
using System.Reflection;

using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Design;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;

using Freeform;
using FGame = Freeform.Game;
using XGame = Microsoft.Xna.Framework.Game;
using FComponent = Freeform.Component;

#endregion


namespace Freeform
{
    /// <summary>
    /// An actor that renders itself as a text line.
    /// </summary>
    public class Actor2D_Text : Actor2D
    {

        #region Members

        /// <summary>
        /// Gets or sets the text for this actor.
        /// </summary>
        public String Text { get; set; }


        /// <summary>
        /// Gets or sets the spritefont of this actor.
        /// </summary>
        public SpriteFont Font { get; set; }


        /// <summary>
        /// Gets or sets whether the actor should use the natural font size to draw.
        /// If true the font size will be scaled by the Entity's NaturalFontScaling value (use Vector2.One for the natural font size.)
        /// Otherwise the size of the font will be resized to be exactly equal to the Entity's size value.
        /// </summary>
        public Boolean UseNaturalFontSize { get; set; }


        /// <summary>
        /// Gets or sets the value to scale the natural font size by if the actor is using the natural font size.
        /// </summary>
        public Vector2 NaturalFontScaling { get; set; }

        #endregion


        #region Init

        /// <summary>
        /// Creates a new 2d text actor.
        /// </summary>
        /// <param name="Entity">The entity that this actor represents.</param>
        public Actor2D_Text(Entity2D Entity)
            : base(Entity)
        {
            Text = "";
            Font = null;
            NaturalFontScaling = Vector2.One;
        }


        /// <summary>
        /// Creates a new 2d text actor.
        /// </summary>
        /// <param name="Entity">The entity that this actor represents.</param>
        /// <param name="Text">The text of the actor.</param>
        /// <param name="Font">The font of the actor.</param>
        public Actor2D_Text(Entity2D Entity, String Text, SpriteFont Font)
            : base(Entity)
        {
            this.Text = Text;
            this.Font = Font;
            NaturalFontScaling = Vector2.One;
        }

        #endregion


        #region Draw

        /// <summary>
        /// Draws the entity onto the viewport.
        /// </summary>
        /// <param name="Viewport">The viewport onto which the entity is being rendered.</param>
        /// <param name="Time">Provides a snapshot of timing values.</param>
        public override void Draw(Viewport2D Viewport, GameTime Time)
        {
            // Check early breaks.
            if (Viewport.SpriteBatch == null) return;
            if (Font == null) return;
            if (Text == null) return;
            if (Entity == null) return;

            Vector2 Draw_Position = Viewport.Camera.TransformPosition(Entity.Position);
            Vector2 Draw_Measure = Font.MeasureString(Text);
            Vector2 Draw_Origin = Draw_Measure / 2;

            // If we're using natural font size we need to adjust the size of the entity.
            if (UseNaturalFontSize)
                Entity.Size = Draw_Measure * NaturalFontScaling;

            Vector2 Draw_Size = Viewport.Camera.TransformSize(Entity.Size);
            Vector2 Draw_Scale = Draw_Size / Draw_Measure;

            Viewport.SpriteBatch.DrawString(Font, Text, Draw_Position, ColourOverlay, Entity.Orientation, Draw_Origin, Draw_Scale, SpriteEffects.None, Depth);
        }

        #endregion

    }
}
